package com.alien.admin.context.task.impl;

import com.alien.admin.context.task.DeployMessageNotifier;
import com.alien.admin.modules.deploy.domain.DeployRec;
import com.alien.admin.modules.deploy.repository.DeployRecRepository;
import com.alien.admin.modules.message.service.MessageNotifier;
import com.alien.admin.utils.DeployConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class DeployMessageNotifierImpl implements DeployMessageNotifier {

    @Autowired
    private DeployRecRepository deployRecRepository;

    @Autowired
    private MessageNotifier messageNotifier;

    @Override
    public void sendDeploymentStatusMessage(DeployRec deployRec) {
        Integer deployStatus = deployRec.getDeployStatus();

        Long userId = deployRec.getOperatorId();
        if(DeployConstant.DEPLOY_STATUS_FAILED.equals(deployStatus)) {
            String message = String.format("您部署的服务[%s]部署失败!", deployRec.getProjectName());
            messageNotifier.sendInfoMessage(userId, "部署失败提醒", message, makeRouter(deployRec));
        } else if(DeployConstant.DEPLOY_STATUS_CANCEL.equals(deployStatus)) {
            String message = String.format("您部署的服务[%s]已经被取消!", deployRec.getProjectName());
            messageNotifier.sendInfoMessage(userId, "部署取消提醒", message, makeRouter(deployRec));
        } else {
            // 判断是否流程成功
            Integer deployStage = deployRec.getStage();
            boolean allFinish = DeployConstant.DEPLOY_STAGE_HEALTH_CHECK.equals(deployStage)
                    && DeployConstant.DEPLOY_STATUS_SUCCESS.equals(deployStatus);
            if(allFinish) {
                String message = String.format("您部署的服务[%s]已经部署完毕!", deployRec.getProjectName());
                messageNotifier.sendInfoMessage(userId, "部署成功提醒", message, makeRouter(deployRec));
            }
        }
    }

    @Async
    @Override
    public void sendDeploymentStatusMessage(Long deployRecId) {
        Map<String, Object> deployStatusMap = deployRecRepository.findDeployStatus(deployRecId);
        if(deployStatusMap != null) {
            String stage = String.valueOf(deployStatusMap.get("stage"));
            String projectId = String.valueOf(deployStatusMap.get("project_id"));
            String deployStatus = String.valueOf(deployStatusMap.get("deploy_status"));
            String projectName = String.valueOf(deployStatusMap.get("project_name"));
            String operatorId = String.valueOf(deployStatusMap.get("operator_id"));

            DeployRec deployRec = new DeployRec();
            deployRec.setId(deployRecId);
            deployRec.setProjectId(Integer.valueOf(projectId));
            deployRec.setStage(Integer.valueOf(stage));
            deployRec.setDeployStatus(Integer.valueOf(deployStatus));
            deployRec.setProjectName(projectName);
            deployRec.setOperatorId(Long.valueOf(operatorId));
            sendDeploymentStatusMessage(deployRec);
        }
    }

    private String makeRouter(DeployRec deployRec) {
        return String.format("/deploy/proccess?projectId=%d&deployRecId=%d", deployRec.getProjectId(), deployRec.getId());
    }

}
